﻿// --------------------------------
// <copyright file="SourceCodeGeneratorTest.cs" company="TRONSoft">
//     Copyright (c) TRONSoft.
// </copyright>
// <license>
//   This source code is subject to terms and conditions of the Microsoft
//   Public License. A copy of the license can be found in the License.html
//   file at the root of this distribution. If you cannot locate the
//   Microsoft Public License, please send an email to dlr@microsoft.com.
//   By using this source code in any fashion, you are agreeing to be bound 
//   by the terms of the Microsoft Public License. You must not remove this
//   notice, or any other, from this software.
// </license>
// <author>Tron</author>
// ---------------------------------

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tronsoft.Actucal.Tables;

namespace Tronsoft.Actucal.CalculationEngineCreator.Test
{    
    /// <summary>
    ///This is a test class for SourceCodeGeneratorTest and is intended
    ///to contain all SourceCodeGeneratorTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SourceCodeGeneratorTest
    {
        private CalculationModel model;

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void MyTestInitialize()
        {
            CalculationParameterCollection collection = new CalculationParameterCollection();
            collection.Add(new CalculationParameter<double>("p1", 1));
            collection.Add(new CalculationParameter<double>("p2", 10));
            collection.Add(new CalculationParameter<double>("p3", 10));

            model = new CalculationModel(collection);
            model.Constants.Add(new NameValueCombination<int>("const1", 1));
            model.Constants.Add(new NameValueCombination<DateTime>("const2", DateTime.Now));
            model.Constants.Add(new NameValueCombination<String>("const3", "Hello world"));

            model.Calculations.Add(new Calculation(new Formula("Test", "return (1 + ${p1}) * ${p2}")));
            model.Calculations.Add(new Calculation(new Formula("Test2", 
@"
for i as integer = 0 to 10
    i+=i;
next

return i * ${p3} + Test")));

            TableDefinition table = TableManager.Instance.Tables.Add("table1");
            table.Columns.Add("Col1", typeof(int));
            table.Columns.Add("Col2", typeof(string));
            table.Columns.Add("Col3", typeof(double));
            table.Columns.Add("Col4", typeof(string));

            table.AddTableRow(1, "Piet lut", 12.89, "Suiker");
            table.AddTableRow(2345, "Kees Kabel", 1.8744, "zout");
        }
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void Should_Generate_Source()
        {
            SourceCodeGenerator sg = new SourceCodeGenerator(model);
            string retval = sg.GetSourceCode();

            Assert.AreNotEqual(0, retval.Length);
        }
        
        /// <summary>
        ///A test for SourceCodeGenerator Constructor
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Constructor_Should_Not_Accept_Null()
        {
            SourceCodeGenerator target = new SourceCodeGenerator(null);
        }

        [TestMethod()]
        public void Constructor_Should_Accept_Model()
        {
            SourceCodeGenerator target = new SourceCodeGenerator(model);
        }
    }
}
